<?php

class academic_IndexController extends Zend_Controller_Action
{
    public function init()
    {
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	$db = new academic_Model_DbTable_DbAcademic();
    	$datas = $db->getAcademicShow();
    	$this->view->getdata = $datas;
    	if($this->getRequest()->isPost()){
    		$data = $this->getRequest()->getPost();
    		if(isset($data['status'])){
    			$ids = $data['status'];
    			$db->updateUnStatus($ids);
    			$this->_redirect("/academic/");
    		}elseif (isset($data['unstatus'])){
    			$ids = $data['unstatus'];
    			$db->updateStatus($ids);
    			$this->_redirect("/academic/");
    		}
    		$id = $data['checkBox'];
    		if(isset($data['update'])&& $id !=""){
    			$this->_redirect("/academic/index/edit/id/".$id);
    		}elseif(isset($data['delete'])&& $id !=""){
    			$db->deleteYear($id);
    			Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
    			Application_Form_FrmMessage::redirectUrl('/academic/');
    		}else { $this->_redirect("/academic/");}
    	
    	}
    	
	}
	public function addAction(){
		$form = new academic_Form_FrmAcademic();
		$this->view->frmacademic = $form->FrmAcademic();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			//print_r($data);exit();
			$db = new academic_Model_DbTable_DbAcademic();
			if(isset($data['save'])){
				$db->addAcademic($data);
				Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/academic/index/add');
			}
			elseif(isset($data['save_close'])){
				$db->addAcademic($data);
				Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/academic/');
			}
		}
		
	}
	public function editAction(){
		$id = $this->getRequest()->getParam("id");
		$db = new academic_Model_DbTable_DbAcademic();
		$data = $db->getAllAcademin($id);
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			//print_r($data);exit();
			if(isset($data['save'])){
				$db->updatYear($data, $id);
				Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/academic/index/add');
			}
			if(isset($data['save_close'])){
				$db->updatYear($data, $id);
				Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/academic/');
			}
		}
		$form = new academic_Form_FrmAcademic();
		$this->view->frmacademic = $form->FrmAcademic($data);
	}
	public function deleteAction(){
		$id = $this->getRequest()->getParam('id');
		$db = new academic_Model_DbTable_DbAcademic();
		$db->deleteYear($id);
		Application_Form_FrmMessage::message("កាលុបប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
		$this->_redirect('/academic/');
	}
	public function getfillteraddAction(){
		if($this->getRequest()->IsPost()){
			$data = $this->getRequest()->getPost();
			$year_name = $data['year_name'];
			$sql= "SELECT year_name FROM tbwu_year WHERE year_name = '$year_name'";
			$db = new Application_Model_DbTable_DbGlobal();
			$row = $db->getGlobalDbRow($sql);
			echo Zend_Json::encode($row);
			exit();
		}
	}
}

